package com.dlmz.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.dlmz.util.BaseDao;

@SuppressWarnings("serial")
public class GeneralManage extends JFrame implements ActionListener{
	public static void main(String[] args)   
    {    
        new GeneralManage();  
    } 

    JLabel subjectLab = new JLabel("科系:");  
    JTextField subjectTxt = new JTextField(20);  
    JButton queryBtn = new JButton("查询");  
    private JScrollPane jsp;
	private JTable resultTb; 
	private GeneralModel sm;
	String sql = "SELECT * from st_count_avg_view";  
	public GeneralManage()  
    {  
        super("一般统计");
        /***************************初始化面板控件***********************/  
      //========查询栏  
        
        JPanel top = new JPanel();  

        top.add(subjectLab);  
        top.add(subjectTxt);  
        top.add(queryBtn);  

      //......添加查询栏监听  
        queryBtn.addActionListener((ActionListener) this);  
        queryBtn.setActionCommand("query");  //确定使用的组件
        
        sm = new GeneralModel();  
        
        sm.querygeneral(sql);  
        resultTb = new JTable(sm);  
        jsp = new JScrollPane(resultTb);  
      
        
        this.add(top,BorderLayout.NORTH); 
        //......构建整体布局    
        this.add(jsp,BorderLayout.CENTER);  
        //========设置窗口属性  
        validate();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(1000,400);
		this.setLocationRelativeTo(null);
		setVisible(true);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				if (JOptionPane.showConfirmDialog(GeneralManage.this, "是否退出统计？")==JOptionPane.OK_OPTION) {
					dispose();
					new StatisJframe();
				}else {
					new GeneralManage();
				}
			}
		});
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals("query")) {  
            /*********************查询***********************/  
            //========获取输入科系  
            String subject = subjectTxt.getText().trim();

            if(subject.length() != 0) {  
                //========科系输入有效时，执行查询  
                //......定义参数  
                String newSql = sql +" where Sdept=? "; 
                //......更新模型  
                jtableUpdate(newSql,subject);  
            } else {  
            	jtableUpdate(sql);  
            }  
        }
	}
	
	 //========更新JTable内数据  
    public void jtableUpdate(String sql,Object...objects)  
    {  
        //......创建模型  
        sm = new GeneralModel();  
        sm.querygeneral(sql,objects);  
        //......更新显示  
        resultTb.setModel(sm);  
    } 
}
